'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            $scope.defaultImgData={};
            if($state.includes('**.menu.update')){
                title="编辑菜单信息";
                var menuNo = $state.params.id;
                activate(menuNo);
                $scope.isDisabled = false;
            }else if($state.includes('**.menu.create')){
                title="添加菜单";
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;

                // if(!$('#moduleNo').val()){
                //     $scope.alertModal({'content':'模块编号不能为空'});
                //     return;
                // }

                if(!$('#menuName').val()){
                    $scope.alertModal({'content':'菜单名称不能为空'});
                    return;
                }
                if(!$('#menuType').val()){
                    $scope.alertModal({'content':'菜单类型不能为空'});
                    return;
                }
                if(!$('#requestFlag').val()){
                    $scope.alertModal({'content':'跳转标识不能为空'});
                    return;
                }
                if(!$('#requestUrl').val()){
                    $scope.alertModal({'content':'跳转URL不能为空'});
                    return;
                }
                if(!$('#moduleNo').val()){
                        $scope.alertModal({'content':'模块编号不能为空'});
                        return;
                    }
                if(!$('#channelNo').val()){
                    $scope.alertModal({'content':'渠道编号不能为空'});
                    return;
                }
                if(!$('#sortNum').val()){
                    $scope.alertModal({'content':'排序号不能为空'});
                    return;
                }
                if(!$('#enable').val()){
                    $scope.alertModal({'content':'是否可用不能为空'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                $scope.record.icon = picUrl[0];

                var defaultPicUrl = $scope.imgDatas[0].src;
                $scope.record.defaultIcon = defaultPicUrl[0];
                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.menuNo ? $scope.app.project_name+'/messageSets/menu/update' : $scope.app.project_name+'/messageSets/menu/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.messageSets.menu.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            // 初始化页面
            function activate(menuNo) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/messageSets/menu/detail',
                    data: {'menuNo': menuNo}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.imgData = [];
                        $scope.imgDatas = [];
                        var srcArr = [];
                        srcArr.push($scope.record.icon);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        var srcArrs = [];
                        srcArrs.push($scope.record.defaultIcon);
                        $scope.imgDatas.push({
                            src:srcArrs
                        })
                        console.log($scope.imgDatas)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

        }]);